package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserFollowsMapper {
    // 关注用户
    @Insert("INSERT INTO follow (follower_id, followed_id) VALUES (#{followerId}, #{followedId})")
    int followUser(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

    // 取消关注用户
    @Delete("DELETE FROM follow WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int unfollowUser(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

    // 获取用户的关注者数量
    @Select("SELECT COUNT(*) FROM follow WHERE followed_id = #{userId}")
    int getFollowerCount(@Param("userId") Integer userId);

    // 获取关注者列表
    @Select("SELECT u.id, u.username, u.account, u.password, u.email, u.gender, u.birthday, u.bio, u.avatar, " +
            "u.created_at AS createdAt, u.updated_at AS updatedAt " +
            "FROM user u " +
            "JOIN follow f ON u.id = f.follower_id " +
            "WHERE f.followed_id = #{userId}")
    List<User> getFollowers(@Param("userId") Long userId);

    // 获取当前用户关注的人的列表
    @Select("SELECT u.id, u.username, u.account, u.password, u.email, u.gender, u.birthday, u.bio, u.avatar, " +
            "u.created_at AS createdAt, u.updated_at AS updatedAt " +
            "FROM user u " +
            "JOIN follow f ON u.id = f.followed_id " +
            "WHERE f.follower_id = #{followerId}")
    List<User> getFollowedUsers(@Param("followerId") Long followerId);
    // 获取当前用户关注的人的数量
    @Select("SELECT COUNT(*) FROM follow WHERE follower_id = #{followerId}")
    int getFollowedCount(@Param("followerId") Integer followerId);
    //判断是否已经关注
    @Select("SELECT COUNT(*) FROM follow WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int isFollowing(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);
    // 查询帖子发布者的用户 ID
    @Select("SELECT user_id FROM post WHERE id = #{postId}")
    Long getPostOwnerId(@Param("postId") Long postId);
}
